from pathlib import Path


def iter_path(path: Path):
    c_files = []
    h_files = []

    for file_path in path.rglob('*'):
        if 'cmake' in str(file_path).lower():
            continue
        if file_path.suffix in {'.c', '.cpp'}:
            c_files.append(file_path)
        elif file_path.suffix == '.h':
            h_files.append(file_path)

    h_files_dict = {h_file.name: h_file for h_file in h_files}

    output = []
    for c_file in c_files:
        h_file_name = f'{c_file.stem}.h'

        c_file = c_file.resolve()
        if h_file_name in h_files_dict:
            h_file = h_files_dict.pop(h_file_name)
            h_file: Path = h_file.resolve()
            output.append((h_file, c_file))
        else:
            output.append((None, c_file))

    for h_file in h_files_dict.values():
        h_file: Path = h_file.resolve()
        output.insert(0, (h_file, None))

    return output

# todo: 没处理如果同名头文件的情况, 如果同名会只保留最后一个
def filter_c_headers(iter_output):
    iter_output = filter(lambda x: x[0] is not None, iter_output)
    return {h.name: (h, c) for h, c in iter_output}

if __name__ == '__main__':
    project = Path('../example_project/cproject')
    a = iter_path(project)
    print(a)
    b = filter_c_headers(a)
    print(b)
